package Socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client4 {
    private Socket socket;

    public Client4(){
        try {
            System.out.println("正在链接服务端...");
            socket=new Socket("localhost",8080);
            System.out.println("服务端链接成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public void start(){
        try {
            OutputStream out=socket.getOutputStream();

            while (true) {
                Scanner scanner=new Scanner(System.in);
                String line= scanner.nextLine();
                if ("exit".equals(line)) {
                    break;
                }
                byte[] data = line.getBytes();
                out.write(data.length);
                out.write(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (socket != null){
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client4 client=new Client4();
        client.start();
    }
}
